<?php
/**
 * @title 994. 腐烂的橘子
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param Integer[][] $grid
     * @return Integer
     */
    function orangesRotting($grid) {
        /**
         * @since 2024-01-11 行数和列数
         */
        $row = count($grid);
        $col = count($grid[0]);

        /**
         * @since 2024-01-11 新鲜橘子的数量
         */
        $freshCount = 0;

        /**
         * @since 2024-01-11 腐烂橘子的坐标
         */
        $nodeArray = [];
        for($r=0; $r<$row; $r++){
            for($c=0; $c<$col; $c++){
                /**
                 * @since 2024-01-11 新鲜的橘子
                 */
                if($grid[$r][$c] == 1){
                    $freshCount++;
                } elseif($grid[$r][$c] == 2){
                    /**
                     * @since 2024-01-11 腐烂的橘子
                     */
                    $nodeArray[] = [$r, $c];
                } else {}
            }
        }

        /**
         * @since 2024-01-11 没有新鲜橘子
         */
        if($freshCount == 0){
            return 0;
        } else {}

        /**
         * @since 2024-01-11 遍历层级
         */
        $level = 0;

        /**
         * @since 2024-01-05 BFS广度遍历
         */
        while($nodeArray){
            /**
             * @since 2024-01-05 下次遍历元素的集合
             */
            $nextNodeArray = [];

            /**
             * @since 2024-01-05 遍历当前元素集合
             */
            foreach ($nodeArray as list($r, $c)){
                /**
                 * @since 2024-01-11 4个方向
                 */
                $moveArray = [
                    [$r-1, $c],
                    [$r+1, $c],
                    [$r, $c-1],
                    [$r, $c+1],
                ];
                foreach ($moveArray as list($nextR, $nextC)){
                    /**
                     * @since 2024-01-11 存在，且是新鲜橘子
                     */
                    if(isset($grid[$nextR][$nextC]) AND $grid[$nextR][$nextC] == 1){
                        $freshCount--;
                        $grid[$nextR][$nextC] = 2;

                        /**
                         * @since 2024-01-11 下次遍历的点
                         */
                        $nextNodeArray[] = [$nextR, $nextC];
                    } else {}
                }
            }

            /**
             * @since 2024-01-05 下次遍历
             */
            $nodeArray = $nextNodeArray;
            $level++;
        }

        /**
         * @since 2024-01-11 是否还有新鲜橘子
         * @return Integer
         */
        if($freshCount == 0){
            return $level-1;
        } else {
            return -1;
        }
    }
}

/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.11_8.php
 */
$datas = [
    [[[2,1,1],[1,1,0],[0,1,1]]], // 4
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->orangesRotting(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
